<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAllCompanyApi } from '../../api/admin'

onMounted(() => {
  fetchCompanies()
})
// 查询条件
const query = reactive({
  name: '',
  description: '',
  companyScale: ''
})

// 企业列表与分页
const companyList = ref([
  {
  id: null,
  name: '',
  logo: '',
  description: '',
  companyScale: '',
  address: '',
  website: '',
  establishDate: ''
  }
])
//分页
const total = ref(20)
const currentPage = ref(1)
const pageSize = ref(6)
const background = ref(true)
//每页大小改变时触发
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  fetchCompanies()
}
//当前页码改变时触发
const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchCompanies()
}
// 弹窗控制
const dialogVisible = ref(false)
const form = reactive({
  id: null,
  name: '',
  logo: '',
  description: '',
  companyScale: '',
  address: '',
  website: '',
  establishDate: ''
})
const formRules = {
  name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }]
}
const formRef = ref(null)

// 查询企业列表
const fetchCompanies = async () => {
  const result = await getAllCompanyApi(query.name, query.description, query.companyScale, currentPage.value, pageSize.value)
  if(result.code){
    console.log(result.data)
    companyList.value = result.data.rows
    total.value = result.data.total
  }else{
    ElMessage.error(result.msg)
  }
}

// 查询按钮
const handleSearch = () => {
  currentPage.value = 1
  fetchCompanies()
}

const dialogTitle = ref('')
// 企业详情
const openDetailDialog = (row) => {
  dialogTitle.value = '企业详情'
  Object.assign(form, row)
  dialogVisible.value = true
}

// 删除企业
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该企业吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    await axios.delete(`/api/companies/${row.id}`)
    ElMessage.success('删除成功')
    fetchCompanies()
  }).catch(() => {})
}

onMounted(fetchCompanies)
</script>

<template>
    <h2>企业列表</h2>
    <div class="company-container">
    <!-- 查询表单 -->
    <el-form :inline="true" class="search-form" @submit.prevent="handleSearch">
      <el-form-item label="企业名称">
        <el-input v-model="query.name" placeholder="请输入企业名称" clearable />
      </el-form-item>
      <el-form-item label="企业描述">
        <el-input v-model="query.description" placeholder="请输入企业描述" clearable />
      </el-form-item>
      <el-form-item label="企业规模">
        <el-input v-model="query.companyScale" placeholder="请输入企业规模" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 企业列表 -->
    <el-table :data="companyList" class="company-table" height="500px" border stripe>
      <el-table-column prop="id" label="ID" width="60" align="center" />
      <el-table-column prop="name" label="企业名称" min-width="100" />
      <el-table-column prop="logo" label="Logo" width="80" align="center">
        <template #default="{ row }">
          <img :src="row.logo" alt="logo" class="company-logo" />
        </template>
      </el-table-column>
      <el-table-column prop="description" label="企业描述" min-width="180" />
      <el-table-column prop="companyScale" label="企业规模" width="100" />
      <el-table-column prop="address" label="企业地址" min-width="120" />
      <el-table-column prop="website" label="企业官网" min-width="120" />
      <el-table-column prop="establishDate" label="成立日期" width="120" />
      <el-table-column label="操作" width="160" align="center">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="openDetailDialog(row)">企业详情</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  <!-- 分页 -->
<div class="container">
  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[6, 15, 20]"
    :size="size"
    :background="background"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</div>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" :close-on-click-modal="false">
      <el-form :model="form" :rules="formRules" ref="formRef" label-width="100px">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="Logo">
          <el-input v-model="form.logo" placeholder="图片URL" />
        </el-form-item>
        <el-form-item label="企业描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item label="企业规模">
          <el-input v-model="form.companyScale" />
        </el-form-item>
        <el-form-item label="企业地址">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item label="企业官网">
          <el-input v-model="form.website" />
        </el-form-item>
        <el-form-item label="成立日期">
          <el-date-picker v-model="form.establishDate" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.company-container {
  margin-top: 20px;
  background: #fff;
  padding: 24px;
  border-radius: 8px;
  min-height: 600px;
  box-shadow: 0 2px 8px #f0f1f2;
}
.search-form {
  margin-bottom: 20px;
  padding: 16px 0 0 0;
  background: #f7f8fa;
  border-radius: 6px;
}
.company-table {
  margin-top: 10px;
}
.company-logo {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  object-fit: cover;
  border: 1px solid #eee;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>